package com.itzxx.huazhun.ui.user.audit

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.chad.library.adapter4.QuickAdapterHelper
import com.chad.library.adapter4.loadState.LoadState
import com.chad.library.adapter4.loadState.trailing.TrailingLoadStateAdapter
import com.itzxx.huazhun.R
import com.itzxx.huazhun.databinding.ActivityAdministratorsAuditBinding
import com.itzxx.huazhun.view.adapter.CustomLoadMoreAdapter
import com.itzxx.library_base_ktx.base.activity.BaseVmDbActivity
import com.itzxx.library_base_ktx.ext.util.loge
import com.itzxx.library_base_ktx.ext.view.addLinearAdapter
import com.itzxx.library_base_ktx.view.BaseDialog
import com.itzxx.library_base_ktx.view.InputDialog

class AdministratorsAuditActivity :
    BaseVmDbActivity<AdministratorsAuditViewModel, ActivityAdministratorsAuditBinding>() {

    companion object {
        fun startActivity(context: Context) = context.startActivity(
            Intent(
                context,
                AdministratorsAuditActivity()::class.java
            )
        )
    }

    private lateinit var helper: QuickAdapterHelper
    private lateinit var administorsAuditAdapter: AdministorsAuditAdapter
    private lateinit var loadMoreAdapter: CustomLoadMoreAdapter

    override fun initView(savedInstanceState: Bundle?) {
        setSupportActionBar(mDatabind.tbHead)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        initRecycview()
    }

    private fun initRecycview() {
        administorsAuditAdapter = AdministorsAuditAdapter()
        administorsAuditAdapter.addOnItemChildClickListener(R.id.tv_delete) { adapter, _, position ->
            "onItemChildClick:  add $position".loge()
            InputDialog.Builder(this).setTitle("拒绝理由")
                .setListener(object : InputDialog.OnListener {
                    override fun onConfirm(dialog: BaseDialog?, content: String) {
                        "onConfirm".loge()
                        val item = adapter.getItem(position)
                        item?.let {
                            if (TextUtils.isEmpty(content)) {
                                mViewModel.toast("请输入拒绝理由")
                            } else {
                                item.refuseRemark = content
                                mViewModel.doUserAuditHandle(it, 1, position)
                            }
                        }
                    }
                })
                .show()
        }

        administorsAuditAdapter.addOnItemChildClickListener(R.id.tv_audit_pass) { adapter, _, position ->
            "onItemChildClick:  add $position".loge()
            val item = adapter.getItem(position)
            item?.let {
                item.refuseRemark = ""
                mViewModel.doUserAuditHandle(it, 0, position)

            }
        }
        loadMoreAdapter = CustomLoadMoreAdapter()
        loadMoreAdapter.setOnLoadMoreListener(object : TrailingLoadStateAdapter.OnTrailingListener {
            override fun onLoad() {
                // 执行加载更多的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun onFailRetry() {
                // 加载失败后，点击重试的操作，通常都是网络请求
                mViewModel.getList(false)
            }

            override fun isAllowLoading(): Boolean {
                return true
            }
        })
        helper = QuickAdapterHelper.Builder(administorsAuditAdapter)
            .setTrailingLoadStateAdapter(loadMoreAdapter)
            .build()
        helper.trailingLoadState = LoadState.None
        mDatabind.rvList.addLinearAdapter(helper.adapter)
    }

    override fun createObserver() {
        mViewModel.run {
            listDataUiStateLiveData.observe(this@AdministratorsAuditActivity) {
                if (it.isRefresh) {
                    administorsAuditAdapter.submitList(it.listData)
                } else {
                    administorsAuditAdapter.addAll(it.listData)
                }

                if (it.hasMore) {
                    helper.trailingLoadState = LoadState.NotLoading(false)
                } else {
                    loadMoreAdapter.setFirstNotFullScreen(it.isRefresh)
                    helper.trailingLoadState = LoadState.NotLoading(true)
                }
            }
            singleListDataUiStateLiveData.observe(this@AdministratorsAuditActivity) {
//                administorsAuditAdapter.notifyItemChanged(it.position, it.data)
                administorsAuditAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun databind2ViewModel() {
        super.databind2ViewModel()
        mDatabind.viewmodel = mViewModel
    }

    override fun isApplyDefaultWaitDialog(): Boolean {
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        finish()
        return true
    }
}